package dbutils;

import com.mysql.jdbc.exceptions.MySQLSyntaxErrorException;

import exttable.SGTTestParametrs;
import exttable.formgenerator.MessageFactory;
import exttable.login.StartPanel;

import javax.swing.*;

import options.AppSettings;
import options.SettingsFrame;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Dec 25, 2009
 * Time: 1:28:27 AM
 * To change this template use File | Settings | File Templates.
 */
public class DbUtils {
    public static  String GENERAL_DATABASE_NAME = "";
    public static  String SECONDARY_DATABASE_NAME="";
    private static Connection con = null;
    private static String ipAddress;
    private static String host=AppSettings.get("dataBaseConnectionSettings", "host","localhost").toString();
    private static String port=AppSettings.get("dataBaseConnectionSettings", "port","3306").toString();
    private static String user=AppSettings.get("dataBaseConnectionSettings", "user","root").toString();
    private static String pass=AppSettings.get("dataBaseConnectionSettings", "password","").toString();
    public static Map<String,String> mapDataBases=getMapDataBases();
    public static String ModeOfOperation="";


    public static String getIpAddress() {
        return ipAddress;
    }

    public static String getPass() {
        return pass;
    }

    public static Connection getDBConnectionGeneral() throws ClassNotFoundException, SQLException {
        try {
            ipAddress = SGTTestParametrs.getInstance().getServerAddress();
            if (ipAddress == null || ipAddress.length() == 0) {
                ipAddress = "localhost";
            }
            Class.forName("com.mysql.jdbc.Driver");

//                con = DriverManager.getConnection("jdbc:mysql://" + host+":"+port+ "/" + DbUtils.GENERAL_DATABASE_NAME, user,pass);
            con = DriverManager.getConnection("jdbc:mysql://" + AppSettings.get("dataBaseConnectionSettings", "host","localhost").toString()
                    +":"+AppSettings.get("dataBaseConnectionSettings", "port","3306").toString()+ "/"
                    + DbUtils.GENERAL_DATABASE_NAME, AppSettings.get("dataBaseConnectionSettings", "user","root").toString(),AppSettings.get("dataBaseConnectionSettings", "password","").toString());
//            }
            return con;
        }
        catch (MySQLSyntaxErrorException e){
            e.printStackTrace();
            con.close();
            JOptionPane.showMessageDialog(null,"Unknown database");
            throw new MySQLSyntaxErrorException("Unknown database");

        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            con.close();
            throw new ClassNotFoundException("no driver");

        }
        catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"connection failed");
            if(MessageFactory.areYouSure("do you want to configurate connection settings")){
                SettingsFrame settingsFrame=new SettingsFrame();
                settingsFrame.setLogIn(false);
            }
            e.printStackTrace();
            con.close();
            throw new SQLException("server error");

        }
    }

    public static Connection getDBConnectionSecondary() throws ClassNotFoundException, SQLException {
        try {
            ipAddress = SGTTestParametrs.getInstance().getServerAddress();
            if (ipAddress == null || ipAddress.length() == 0) {
                ipAddress = "localhost";
            }
            Class.forName("com.mysql.jdbc.Driver");

//                con = DriverManager.getConnection("jdbc:mysql://" + host+":"+port+ "/" + DbUtils.GENERAL_DATABASE_NAME, user,pass);
            con = DriverManager.getConnection("jdbc:mysql://" + AppSettings.get("dataBaseConnectionSettings", "host","localhost").toString()
                    +":"+AppSettings.get("dataBaseConnectionSettings", "port","3306").toString()+ "/"
                    + DbUtils.SECONDARY_DATABASE_NAME, AppSettings.get("dataBaseConnectionSettings", "user","root").toString(),AppSettings.get("dataBaseConnectionSettings", "password","").toString());
//            }
            return con;
        }
        catch (MySQLSyntaxErrorException e){
            e.printStackTrace();
            con.close();
            JOptionPane.showMessageDialog(null,"Unknown database");
            throw new MySQLSyntaxErrorException("Unknown database");

        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            con.close();
            throw new ClassNotFoundException("no driver");

        }
        catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"connection failed");
            if(MessageFactory.areYouSure("do you want to configurate connection settings")){
                SettingsFrame settingsFrame=new SettingsFrame();
                settingsFrame.setLogIn(false);
            }
            e.printStackTrace();
            con.close();
            throw new SQLException("server error");

        }
    }


    private static Map<String,String> getMapDataBases(){
        Map<String,String> mapDataBases=new HashMap<String, String>();
        try {
            Connection conn=DbUtils.getDBConnectionGeneral();
            PreparedStatement ps=conn.prepareStatement("show databases");
            ResultSet rs=ps.executeQuery();
            while (rs.next()){
                mapDataBases.put(rs.getString(1), rs.getString(1));
            }
            rs.close();
            ps.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return mapDataBases;
    }
}
